package com.socialsoftware.ui.Me;

import android.app.Application;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.socialsoftware.model.User;
import com.socialsoftware.util.SessionManager;

public class MeViewModel extends AndroidViewModel {

    private final MutableLiveData<String> mUserName;
    private final MutableLiveData<String> mWechatId;
    private final MutableLiveData<String> mUserId;
    private final MutableLiveData<String> mAvatarUrl;
    private final MutableLiveData<Integer> mNewMomentsCount;
    private final MutableLiveData<Integer> mFavoritesCount;
    private final MutableLiveData<Integer> mEmojiCount;
    private final MutableLiveData<String> mUsername;
    private SessionManager sessionManager;

    public MeViewModel(Application application) {
        super(application);
        mUserName = new MutableLiveData<>();
        mWechatId = new MutableLiveData<>();
        mUserId = new MutableLiveData<>();
        mAvatarUrl = new MutableLiveData<>();
        mNewMomentsCount = new MutableLiveData<>();
        mFavoritesCount = new MutableLiveData<>();
        mEmojiCount = new MutableLiveData<>();
        mUsername = new MutableLiveData<>();
        
        // Initialize with data from SessionManager
        sessionManager = SessionManager.getInstance(application);
        loadUserData();
    }
    
    private void loadUserData() {
        User user = sessionManager.getUser();
        if (user != null) {
            mUserName.setValue(user.getDisplayName() != null ? user.getDisplayName() : user.getUsername());
            mWechatId.setValue("账号: " + user.getUsername());
            mUserId.setValue(user.getId());
            mAvatarUrl.setValue(user.getAvatarUrl());
            mUsername.setValue(user.getUsername());
        } else {
            // Fallback to default values if user is not logged in
            mUserName.setValue("未登录用户");
            mWechatId.setValue("账号: 请登录");
            mUserId.setValue("");
            mAvatarUrl.setValue("");
            mUsername.setValue("");
        }
        
        // Demo values for other data
        mNewMomentsCount.setValue(2); // 2 new moments updates
        mFavoritesCount.setValue(15); // 15 favorites
        mEmojiCount.setValue(42); // 42 emojis
    }

    public LiveData<String> getUserName() {
        return mUserName;
    }

    public LiveData<String> getWechatId() {
        return mWechatId;
    }
    
    public LiveData<String> getUserId() {
        return mUserId;
    }
    
    public LiveData<String> getAvatarUrl() {
        return mAvatarUrl;
    }
    
    public LiveData<String> getUsername() {
        return mUsername;
    }
    
    public LiveData<Integer> getNewMomentsCount() {
        return mNewMomentsCount;
    }
    
    public LiveData<Integer> getFavoritesCount() {
        return mFavoritesCount;
    }
    
    public LiveData<Integer> getEmojiCount() {
        return mEmojiCount;
    }
    
    public void clearNewMomentsCount() {
        mNewMomentsCount.setValue(0);
    }
    
    // Legacy method kept for compatibility
    public LiveData<String> getText() {
        return mUserName;
    }
}